/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fanta.framework.dao;
import fanta.framework.manager.ConnectionManager;
import fanta.framework.persistence.Giocatore;
import fanta.framework.persistence.Lega;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.EntityManager;
import org.apache.log4j.Logger;

/**
 *
 * @author Raizer
 */
public class LegaDAO {
 private static final Logger logger = Logger.getLogger(LegaDAO.class);
 private static Lega creaLegaDAO(String nome, int maxteam, Date datacreazione, Date datachiusura, ConnectionManager cm){
       Lega ut = new Lega();
       EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try {
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            ut.setNome(nome);
            ut.setMaxteam(maxteam);
            ut.setDatacreazione(datacreazione);
            ut.setDatachiusura(datachiusura);
            entityManager.persist(ut);
            logger.debug("Transaction persisted");
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            logger.info("Nuovo Lega:"+ut);

        } catch (Exception e) {
            logger.error("Errore in LegaDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }
        return ut;
}
public static boolean AggiornaLega(Lega lega, ConnectionManager cm) {
        boolean result = false;
            EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try {
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            entityManager.persist(entityManager.merge(lega));
            logger.debug("Transaction persisted");
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            result = true;
        } catch (Exception e) {
            logger.error("Errore in AggiornaLegaDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }
        return result;
    }
    public static boolean cancellaLega(String nome, ConnectionManager cm) {
        boolean result = false;
        EntityManager entityManager = (EntityManager) cm.getEntityManager();
        try{
            entityManager.getTransaction().begin();
            logger.debug("Start Transaction");
            entityManager.remove(entityManager.find(Lega.class, nome));
            entityManager.getTransaction().commit();
            logger.debug("Transaction committed");
            result = true;
        } catch (Exception e) {
            logger.error("Errore in cancellaLegaDAO", e);
            logger.debug("##EXCEPTION## Transaction will be roll back");
            entityManager.getTransaction().rollback();
            logger.debug("Transaction rolled back!");
        }

        return result;
    }
    public static Lega cercaLegaByNome(String nome, ConnectionManager cm) {
        EntityManager entityManager = (EntityManager) cm.getEntityManager();
        Lega ut = new Lega();
        try {
            Object obj = entityManager.find(Lega.class, nome);
            if(obj != null) {
                ut = (Lega)obj;
            }
        }catch (Exception e) {
            logger.error("Errore in CercaLegaDAO", e);
        }
        return ut;
   }
}

